package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.*;
import com.woniuxy.entity.*;
import com.woniuxy.entity.Course;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Training;
import com.woniuxy.entity.dto.TrainingDto;
import com.woniuxy.entity.dto.UserTrainingDto;
import com.woniuxy.service.TrainingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.ArrayList;

/**
 * @Author：jojo
 * @Date：2022/3/7 14:45
 * @Description :TODO
 */
@Service
public class TrainingsServiceImpl implements TrainingsService {
    @Autowired(required = false)
    TrainingsDao trainingsDao;
    @Autowired(required = false)
    CourseDao courseDao;
    @Autowired(required = false)
    AccessoryDao accessoryDao;
    @Autowired(required = false)
    ProgressingDao progressingDao;
    @Autowired(required = false)
    TopicDao topicDao;
    /**
     * Auther: gaoyi  <br/>
     * Date: 2022/3/7:16:02  <br/>
     * Description:查询培训项目
     */
    @Override
    public ResponseEntity findTraining(int currentPage, int pageSize, Training training) {
        //分页
        PageHelper.startPage(currentPage, pageSize);
        List<Training> trainingsList = trainingsDao.findTraining(training);
        PageInfo<Training> of = PageInfo.of(trainingsList);
        ResponseEntity<PageInfo> listResponseEntity = new ResponseEntity<>();
        if (trainingsList.size()>0) {
            listResponseEntity.setData(of);
            listResponseEntity.setCode(200);
            listResponseEntity.setMsg("查询成功！");
        }else{
            listResponseEntity.setData(null);
            listResponseEntity.setCode(500);
            listResponseEntity.setMsg("数据为空");
        }
        return listResponseEntity;
    }

    /**
     * Auther: gaoyi  <br/>
     * Date: 2022/3/7:19:05  <br/>
     * Description:删除培训项目
     */
    @Override
    public ResponseEntity deleteOne(int id) {
        int num = trainingsDao.deleteById(id);
        if(num>0){
            return new ResponseEntity(200, "成功删除一行数据！", null);
        }else {
            return new ResponseEntity(500,"系统维护中",null);
        }
    }

    /**
     * Auther: gaoyi  <br/>
     * Date: 2022/3/7:19:10  <br/>
     * Description:添加培训项目
     */
    @Override
    public ResponseEntity addOne(TrainingDto trainingDto) {
        //从TrainingDto对象中获取Training对象
        Training training = trainingDto.getTraining();
        int num = trainingsDao.addOne(training);
        if(num>0){
            //获取TrainingDto中选取课程的id
            for(String str:trainingDto.getCourseidList()){
                Course course =new Course();
                course.setId(Integer.parseInt(str)).setStatus("1");
                courseDao.updateCourse(course);
               int num1= trainingsDao.addCourseid(Integer.parseInt(str),training.getId());
               if (num1<0){
                   return ResponseEntity.error("课程-培训表插入失败");
               }
            }
            return ResponseEntity.success("添加成功");
        }
        return ResponseEntity.error("添加失败");
    }



    /**
     * Auther: gaoyi  <br/>
     * Date: 2022/3/7:19:25  <br/>
     * Description:修改培训项目
     */
    @Override
    public ResponseEntity updateById(Training training) {
        int num = trainingsDao.updateById(training);
        if (num > 0) {
            return new ResponseEntity(200, "成功修改一行数据！", null);
        } else {
            return new ResponseEntity(500, "系统维护中", null);
        }
    }



    /**
     * Auther: gaoyi  <br/>
     * Date: 2022/3/9:19:31  <br/>
     * Description:通过trainingId查询crouse
     */
    @Override
    public ResponseEntity findCrouseBytrainingId(Training training) {
        //从中间表获取该培训下所有课程id
        List<Integer> ids = trainingsDao.findCrouseIdBytrainingId(training);
        //通过获取的id查出该课程
        List<Course> courseList = courseDao.findByIds(ids);
        //通过课程中附件字段查出所有附件
        for (Course course : courseList) {
            //获取当前课程的附件字段(1,2,3)
            String acsStr = course.getAccessoryid();
            //分割字段，存为list<Integer>
            List<Integer> acsIdList = new ArrayList<>();
            for (String str : acsStr.split(",")) {
                acsIdList.add(Integer.valueOf(str));
            }
            List<Accessory> accessoryList = accessoryDao.findAccessory(acsIdList);
            course.setAccessoryList(accessoryList);
        }
        return ResponseEntity.success("查询成功", courseList);
    }

    @Override
    public ResponseEntity findTrainingByUserId(User user) {
        List<Training> trainingList =new ArrayList<>();
        //查出当前用户参加的所有培训id
        List<Integer> trainingIdList = trainingsDao.findTrainingByUserId(user);
        for (Integer trainingId:trainingIdList) {
            //查询出所有培训
            List<Training> oneTraining =  trainingsDao.findTraining(new Training().setId(trainingId));
            Training training =oneTraining.get(0);
            List<Course> courseList = new ArrayList<>();
            //查出课程id
            List<Integer> ids = trainingsDao.findCrouseIdBytrainingId(training);
            //通过课程id和用户id查出课程查出进度
            for(Integer id:ids){
                List<Course> oneCourse = courseDao.findCourse(new Course().setId(id));
                Course course =oneCourse.get(0);
                Integer progressing =  progressingDao.findCourseProgressingDao(user.getId(), id);
                if (progressing != null) {
                    course.setProgressing(progressing);
                    courseList.add(course);
                }
            }
            training.setCourseList(courseList);
            trainingList.add(training);
        }
        return ResponseEntity.success("查询成功",trainingList);
    }

    @Override
    public ResponseEntity findTrainingById(int currentPage, int pageSize, int id) {
        List<Integer> ids = trainingsDao.findIds(id);
        List<Training> trainingList =new ArrayList<>();
        for(Integer tid:ids){
            Training training = trainingsDao.findT(tid);
            trainingList.add(training);
        }
        PageHelper.startPage(currentPage,pageSize);
        PageInfo<Training> of = PageInfo.of(trainingList);
        return ResponseEntity.success("success",of);
    }

    @Override
    public ResponseEntity findTrainingBySearch(UserTrainingDto userTrainingDto) {
        List<Training> trainingList =new ArrayList<>();
        //查出当前用户参加的所有培训id
        List<Training> trainings = new ArrayList();
        List<Integer> trainingIdList = trainingsDao.findTrainingByUserId(userTrainingDto.getUser());
        System.out.println(trainingIdList);
        for(Integer id:trainingIdList){
            Training training = trainingsDao.find(id,userTrainingDto.getTraining().getTrainingname(),userTrainingDto.getTraining().getStatus());
            if(training!=null){
                trainings.add(training);
            }
        }

        ResponseEntity responseEntity = new ResponseEntity<>();
        if (trainings.size()>0) {
            responseEntity.setData(trainings);
            responseEntity.setCode(200);
            responseEntity.setMsg("查询成功！");
        }else{
            responseEntity.setData(null);
            responseEntity.setCode(500);
            responseEntity.setMsg("数据为空");
        }

        return responseEntity;
    }

    @Override
    public ResponseEntity findTrainingIdByCrouseId(int id) {
        Integer trainingid = trainingsDao.findTrainingIdByCrouseId(id);
        if(trainingid!=null){
            return ResponseEntity.success("查询成功",trainingid);
        }else {
            return ResponseEntity.error("查询失败");
        }
    }

    /**
     * Auther: gaoyi  <br/>
     * Date: 2022/3/17:10:20  <br/>
     * Description:根据用户id和培训id插入报名信息(报名)
     */
    @Override
    public ResponseEntity apply(int userid, int trainingid) {
            List<Topic> topic= topicDao.findTopicByTrainingId(trainingid);
            int number = (int) (Math.random()*(topic.size()));
            if(number > -1){
                int topicid = topic.get(number).getId();
                int num1 = topicDao.createUserTopic(userid,topicid,trainingid);
                int num2 = topicDao.createUserTraining(userid,trainingid);
                if(num1 > 0 && num2>0){
                    System.out.println("报名成功！");
                    return new ResponseEntity(200, "报名成功", null);
                }else {
                    System.out.println("报名失败！");
                    return new ResponseEntity(500, "报名失败", null);
                }
            }
//        }
        return null;
    }

    @Override
    public ResponseEntity findNotApplyTrainingByUserId(int currentPage,int pageSize,int userid) {
        //分页
        PageHelper.startPage(currentPage, pageSize);
        List<Training> trainingsList = trainingsDao.findNotApplyTrainingByUserId(userid);
        PageInfo<Training> of = PageInfo.of(trainingsList);
        ResponseEntity<PageInfo> listResponseEntity = new ResponseEntity<>();
        if (trainingsList.size()>0) {
            listResponseEntity.setData(of);
            listResponseEntity.setCode(200);
            listResponseEntity.setMsg("查询成功！");
        }else{
            listResponseEntity.setData(null);
            listResponseEntity.setCode(500);
            listResponseEntity.setMsg("数据为空");
        }
        return listResponseEntity;
    }
}
